package toanstt.com;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;
import android.view.animation.AlphaAnimation;

public class Button 
{
	Bitmap bitmap;
	public float scale=1.0f;
	Matrix mat;
	public int x,y;
	int wisize_x;
	int wisize_y;
	int temp_x,temp_y;
	int action,actionCode ;
	public int anpla = 255;
	String string="";
	int str_x,str_y;
	int str_type;
	boolean isstring= false;
	public boolean isdisible = false;
	public boolean nottouch = false;
	
	public Button(Bitmap bitmap2,int x1,int y1,float scale2)
	{
		bitmap = bitmap2;
		x = x1;
		y = y1;
		scale = scale2;
		
		mat = new Matrix();
		wisize_x = (int)(bitmap.getWidth()*scale/2);
		wisize_y = (int)(bitmap.getHeight()*scale/2);
		mat.postScale(scale, scale);
		
		mat.postTranslate(x-wisize_x, y-wisize_y);
		
	}
	public void SetAbletoTouch(boolean a)
	{
		nottouch = !a;
	}
	public void ChangeScale(float continuesclae)
	{
		scale = scale*continuesclae;
		mat = new Matrix();
		wisize_x = (int)(bitmap.getWidth()*scale/2);
		wisize_y = (int)(bitmap.getHeight()*scale/2);
		mat.postScale(scale, scale);
		
		mat.postTranslate(x-wisize_x, y-wisize_y);
	}
	public void ChangePosition(int _x,int _y)
	{
		x = _x;
		y = _y;
		mat.reset();
		mat.postScale(scale, scale);
		mat.postTranslate(x-wisize_x, y-wisize_y);
	}
	public void Draw(Canvas canvas)
	{
		if(isdisible==true)
			anpla = 80;
		
		T.paint_alpha.setAlpha(anpla);
		canvas.drawBitmap(bitmap, mat, T.paint_alpha);
		if(isstring)
		{
			T.paint_alpha.setAlpha(anpla);
			T.bitmapFont[str_type].drawString(canvas, string,str_x,str_y,0);
			T.paint_alpha.setAlpha(255);
		}
	
	}
	public boolean check_click(int x1,int y1)
	{
		if(isdisible==true)
			return false;
		if(x1 > x -wisize_x &&  x1 < x  + wisize_x)
			if(y1 > y -wisize_y &&  y1 < y  + wisize_y)
				return true;
		return false;
	}
	public boolean Kick(MotionEvent mo)
	{
		if(isdisible==true)
			return false;
		
		action = mo.getAction();
		actionCode = action & MotionEvent.ACTION_MASK;
		temp_x = (int)mo.getX();
		temp_y = (int)mo.getY();
		if(check_click(temp_x,temp_y)==true)
		{
			if(actionCode == MotionEvent.ACTION_DOWN || actionCode == MotionEvent.ACTION_MOVE)
			{
				anpla = 100;
				return false;
			}
			else if(actionCode == MotionEvent.ACTION_UP)
				return true;
		
		}
		return false;
	}
	public void SetString(String str)
	{
		string = str;
	}
	public void SetString(String str,int x1,int y1,int typebitmapfont)
	{
		string = str;
		isstring = true;
		str_type= typebitmapfont;
		str_x = x -wisize_x + x1;
		str_y = y-wisize_y + y1;
	}
}
